<?php

namespace api\modules\app\controllers;

use api\components\BaseController;
use api\modules\app\models\MetaArea;
use api\modules\app\models\Qrcode;
use api\modules\app\models\QrcodeRecord;
use api\modules\app\models\Unit;
use api\modules\app\models\UnitHistory;
use api\modules\app\services\HistoryService;
use api\modules\app\services\QrcodeService;
use app\components\Job;
use Yii;

class QrcodeController extends BaseController
{
    public $modelClass = 'api\\modules\\app\\models\\Qrcode';

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['delete'], $actions['create'], $actions['index']);

        return $actions;
    }

    public function actionDelete($id)
    {
        $qrcode = Qrcode::findOne(['id' => $id]);
        if ($qrcode->has_bind) {
            throw new \Exception('请先解除绑定的部件');
        }
        $qrcode->delete();
        return true;
    }

    public function actionInfo()
    {
        $code = $this->getQuery('code');
        $qrcode = Qrcode::findOne(['code' => $code]);
        if ($qrcode->has_bind) {
            $unit = Unit::findOne(['unit_code' => $qrcode->bind_code]);
            if (!empty($unit)) {
                $metas = MetaArea::findAll(['code' => [$unit->district_code, $unit->street_code, $unit->grid_code]]);
                $codes = [];
                foreach ($metas as $meta) {
                    $codes[$meta->code] = $meta->name;
                }
                $unit = $unit->toArray();
                $unit['district_name'] = $codes[$unit['district_code']];
                $unit['street_name'] = $codes[$unit['street_code']];
                $unit['grid_name'] = $codes[$unit['grid_code']];

                $user = $this->getUser();
                HistoryService::log($unit['unit_code'], UnitHistory::TYPE2_UNIT_INSPECT, $user['username'], ['user_id' => $user['id']]);
            }
        }

        return [
            'qrcode' => $qrcode,
            'unit' => $unit ?? [],
        ];
    }

    public function actionIndex()
    {
        $query = $this->getQuery();
        return (new QrcodeService())->list($query);
    }

    public function actionBind()
    {
        $params = $this->getParams();
        $user = $this->getUser();

        return (new QrcodeService())->bind($params['code'], $params, $user);
    }

//    public function actionUnbind() {
//        $params = $this->getParams();
//        $user = $this->getUser();
//
//        return (new QrcodeService())->unbindQrcodeUnit($params, $user);
//    }

    public function actionExport()
    {
        $params = $this->getParams();
        $query = $this->getQuery();
        if (!empty($query['needImage'])) {
            return (new QrcodeService())->export(array_merge($query, $params));
        }
        return (new QrcodeService())->exportCode(array_merge($query, $params));
    }

    public function actionDeleteZip() {
        $params = $this->getQuery();
        if (!isset($params['name']) || empty($params['name'])) {
            return false;
        }
        $path = Yii::$app->getRuntimePath() . DIRECTORY_SEPARATOR . 'qrcode_zip' . $params['name'];
        @unlink($path);
    }


}
